<?php
Yii::import("application.website.phpQuery.phpQuery");
Yii::import("application.website.adapter.WebsiteAdapter");

class WBBIndexWebsiteAdapter extends WebsiteAdapter {
	private $boardID = 0;
	
	/**
	 * Gibt die Filter zurÃ¼ck. 
	 */
	public function matches($url) {
		$params = WebsiteUtil::getParams($url);
		return (!array_key_exists("page", $params) || strtolower($params['page']) == "index");
	}
	
	/**
	 * Versucht, mehrere Foren aus der Website auszulesen und gibt diese dann zurÃ¼ck. 
	 */
	public function parse(Website $website) {
		$board = WebsiteUtil::getBoardByUrl($website->getURL());
		$this->boardID = $board->id;
		phpQuery::newDocumentHTML($website->getContent());
		$archive = new ArchiveBoardCategory();
		foreach(pq("a[id*=boardLink]") as $link) {
			$archive->unsetAttributes();
			$link = pq($link);
			$params = WebsiteUtil::getParams($link->attr("href"));
			$categoryID = (array_key_exists("boardID", $params)) ? $params['boardID'] : 0;
			$description = $link->parent()->parent()->find("p.boardlistDescription")->text();
			$title = $link->text();
			$id = $link->parent()->parent()->parent()->parent()->parent()->parent()->attr("id");
			$parentCategoryID = (strpos("category", $id) == 0) ? intval(str_replace("category", "", $id)) : 0;
			
			//URL zum durchsuchen hinzufÃ¼gen
			WebsiteUtil::addURL($link->attr("href"), true, $board);
			
			//Erstelle
			$archive->categoryID = $categoryID;
			$archive->boardID = $this->boardID;
			$archive->title = $title;
			$archive->parentCategoryID = $parentCategoryID;
			$archive->description = $description;
			
			//Suche
			$found = $archive->find('boardID = "'.$this->boardID.'" AND categoryID = "'.$categoryID.'"');
			if ($found != null) {
				$found->title = $title;
				$found->description = $description;
				$found->parentCategoryID = $parentCategoryID;
				$found->update();
			}else{
				$archive->setIsNewRecord(true);
				$archive->save();
				
	//			echo "Kategorie erstellt: ".$this->boardID.".".$parentCategoryID.".".$categoryID." ".$title."<br>";
			}
		}
	}
	
}
?>
